import pandas as pd
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
from collections import Counter
import json

topk = 20
topsize = 10

keycols = {
    'education_univ': ['../../executive_education_info.csv', 'user_univ'],
    'education_dept': ['../../executive_education_info.csv', 'user_dept'],
    'education_deg': ['../../executive_education_info.csv', 'user_deg'],
    'basic_posn': ['../../executive_basic_info.csv', 'user_posn'],
    'institute_univ': ['../../executive_institute_staff.csv', 'rept_univ'],
    'institute_dept': ['../../executive_institute_staff.csv', 'rept_dept'],
}

output_file = "../sample-data/wordcloud_data.json"


# data = pd.read_csv('../../executive_education_info.csv')  # 替换为你的数据文件路径
# text_column = 'user_univ'  # 替换为你要统计词频的列名
# text_column = 'user_dept'
# text_column = 'user_deg'

# data = pd.read_csv('../../executive_basic_info.csv')  # 替换为你的数据文件路径
# text_column = 'user_posn'  # 替换为你要统计词频的列名

# data = pd.read_csv('../../executive_institute_staff.csv')  # 替换为你的数据文件路径
# text_column = 'rept_univ'  # 替换为你要统计词频的列名
# text_column = 'rept_dept'
# text_column = 'univ_posn'

output_dict = {}

for key in keycols:
    path = keycols[key][0]
    text_column = keycols[key][1]
    data = pd.read_csv(path)

    data[text_column] = data[text_column].apply(lambda x: x if not (pd.isna(x) or x =="None") else '')
    tmp_list = data[text_column].to_list()
    all_list = []
    for word in tmp_list:
        words = word.split(",")
        words = [w for w in words if w != '']
        all_list.extend(words)

    # 统计列表中所有元素的词频
    word_freq = Counter(all_list)
    sorted_word_freq = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)

    output_dict[key] = []
    # 打印词频统计结果
    top_freq = sorted_word_freq[0][1]
    for word, freq in sorted_word_freq[:topk]:
        # print(word, freq)
        output_dict[key].append({'text': word, 'size': (freq*0.8+0.2*top_freq)/top_freq*topsize})
#     print(output_dict[key])
# print(output_dict)

with open(output_file, 'w') as f:
    json.dump(output_dict, f, indent=4, ensure_ascii=False)


